package com.hly.musicbackground.model.admin.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hly.musicbackground.Enum.DelEnum;
import com.hly.musicbackground.Enum.UserRootEnum;
import com.hly.musicbackground.domain.User;
import com.hly.musicbackground.domain.UserLog;
import com.hly.musicbackground.filter.AuthRoot;
import com.hly.musicbackground.model.admin.params.UpdateRootParams;
import com.hly.musicbackground.model.admin.params.VersionParams;
import com.hly.musicbackground.model.user.service.UserLogService;
import com.hly.musicbackground.model.user.service.impl.UserServiceImpl;
import com.hly.musicbackground.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理端用户
 * */
@RestController
@RequestMapping("/api/admin/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "管理端用户")
public class AdminUserController {

    private final UserServiceImpl userService;
    private final UserLogService userLogService;

    /**
     * 冻结、恢复用户
     * @param params 参数
     * */
    @PostMapping("/status")
    @AuthRoot
    public Result status(@RequestBody VersionParams params){
        return Result.ok();
    }

    /**
     * 获取用户列表
     * */
    @GetMapping("/list")
    @AuthRoot
    public Result getUserList(@RequestParam(value = "root", defaultValue = "-1") int root,
                              @RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "300") int limit){
        Page<User> p = userService.lambdaQuery()
                .eq(root != -1, User::getRoot, root)
                .orderByDesc(User::getRoot)
                .page(new Page<>(page, limit));
        return Result.ok(p);
    }

    /**
     * 修改权限
     * @param params 参数
     * */
    @PostMapping("/root")
    @AuthRoot(UserRootEnum.ROOT)
    public Result root(@RequestBody UpdateRootParams params){
        userService.setUserRoot(params.getUserId(), params.getRoot());
        return Result.ok();
    }

    /**
     * 获取用户详情
     * */
    @GetMapping("/detail")
    @AuthRoot
    public Result getLogList(@RequestParam(value = "uid") int uid){
        return Result.ok(userService.getDetail(uid));
    }

    @GetMapping("/log")
    @AuthRoot
    @ApiOperation(value = "获取日志列表")
    public Result getLog(@RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "limit", defaultValue = "10") int limit,
                         @RequestParam(value = "uid", defaultValue = "0") int uid){
        return Result.ok(userLogService.getRecentLogList(page, limit, uid));
    }
}
